﻿$(document).ready(function () {
    var J_light = $("#J_light");
    var J_form = $("#J_form");
    var J_sndapassport_tip = $("#J_sndapassport_tip");
    J_form.find("button[type='submit']").click(function () {
        this.blur();
    });
    //样式
    J_form.find("input[type='text']").focus(function () {
        $(this).addClass("form-text-onfocus");
    }).blur(function () {
        $(this).removeClass("form-text-onfocus");
    });
    //支付方式
    $("#J_payment_more").live("click", function () {
        $(this).parent().toggleClass("form-payment-open").find(".close").slideToggle(500);
    });
    //验证码
    var J_form_captcha = $("#J_form_captcha");
    J_form_captcha.find("img,span").click(function () {
        var url = J_form_captcha.attr("data-url-img") + "?random=" + Math.random();
        J_form_captcha.find("img").attr("src", url);
    }).click();
    J_form_captcha.find("input[type='text']").blur(function () {
        var t = $(this);
        var name = (!t.attr("name")) ? "captcha" : t.attr("name");
        var val = t.val();
        if (val.length < 1) {
            t.nextAll(".form-message").removeClass("form-showsuccess").addClass("form-showerror");
            return;
        };
        var url = J_form_captcha.attr("data-url-validator");
        var parm = { "random": Math.random };
        parm[name] = t.val();
        $.ajax({
            async: true,
            type: "POST",
            url: url,
            data: parm,
            dataType: "text",
            success: function (msg) {
                var data = msg;
                if (data == "true") {
                    t.nextAll(".form-message").removeClass("form-showerror").addClass("form-showsuccess");
                    return;
                }
                t.nextAll(".form-message").removeClass("form-showsuccess").addClass("form-showerror");
                J_form_captcha.find("img").click();
            },
            error: function () {
                alert("抱歉，出错了！请刷新页面后再试。");
            }
        });
    });
    //购买数量
    $("#J_num").keyup(function () {
        //格式
        var t = $(this);
        var val = t.val().replace(/(^\s*)|(\s*$)/g, "");
        var reg = /[^\d]$/;
        if (val.length > 0) {
            val = val.replace(reg, "");
            t.val(val);
        };
        //判定数量
        var all = parseInt(t.attr("data-max"));
        if (isNaN(all)) { all = 0 };
        var per = parseInt(t.val());
        if (isNaN(per)) { per = 0 };
        //报错
        if ((all > 0 && per > all) || per < 1) {
            t.nextAll(".form-message").removeClass("form-showsuccess").addClass("form-showerror");
            return;
        };
        t.nextAll(".form-message").removeClass("form-showerror").addClass("form-showsuccess");
        return;
    }).blur(function () {
        var t = $(this);
        if (t.val().length < 1) {
            t.nextAll(".form-message").removeClass("form-showsuccess").addClass("form-showerror");
            return;
        };
        if (t.nextAll(".form-showerror").length > 0) { return; }
        var url = t.attr("data-verify");
        var parm = { "random": Math.random() };
        parm[t.attr("name")] = t.val();
        var J_price = $("#J_price");
        parm[J_price.attr("name")] = J_price.val();
        $.ajax({
            async: false,
            url: url,
            data: parm,
            method: "post",
            dataType: "text",
            success: function (msg) {
                $("#J_amount").html(msg);
            },
            error: function () {
                alert("抱歉，出错了！请刷新页面后再试。");
            }
        });
    }).focus().blur();
    //盛大通行证
    $("#J_sndapassport").blur(function () {
        J_sndapassport_tip.hide();
        var t = $(this);
        var val = t.val().replace(/^\s+|\s+$/g, "");
        if (val.length < 1) {
            t.nextAll(".form-message").removeClass("form-showsuccess").addClass("form-showerror");
            return;
        };
        var url = t.attr("data-verify");
        var parm = { "random": Math.random() };
        parm[t.attr("name")] = t.val();
        $.ajax({
            async: true,
            type: "POST",
            url: url,
            data: parm,
            dataType: "text",
            success: function (msg) {
                var data = msg;
                if (data == "true") {
                    t.nextAll(".form-message").removeClass("form-showerror").addClass("form-showsuccess");
                    return;
                }
                t.nextAll(".form-message").removeClass("form-showsuccess").addClass("form-showerror");
            },
            error: function () {
                alert("抱歉，出错了！请刷新页面后再试。");
            }
        });
    }).keyup(function () {
        var t = $(this);
        J_sndapassport_tip.show();
        t.nextAll(".form-message").removeClass("form-showsuccess").removeClass("form-showerror");
        $("#J_sndapassport_confirm").nextAll(".form-message").removeClass("form-showsuccess").removeClass("form-showerror");
    }).focus(function () {
        var t = $(this);
        var val = t.val();
        J_sndapassport_tip.show();
        $("#J_sndapassport_confirm").nextAll(".form-message").removeClass("form-showsuccess");
        //如果通行证以及确认通行证一样，则点击后自动全选
        if (val.length > 0 && (val == $("#J_sndapassport_confirm").val())) {
            t.select();
        };
    });
    //盛大通行证确认
    $("#J_sndapassport_confirm").blur(function () {
        var t = $(this);
        var val = t.val();
        var passport = $("#J_sndapassport").val();
        if (val.length < 1 || passport != val) {
            t.nextAll(".form-message").removeClass("form-showsuccess").addClass("form-showerror");
            return;
        };
        t.nextAll(".form-message").removeClass("form-showerror").addClass("form-showsuccess");
    });
    //初始化表单状态
    $("#J_form_check").val("0");
    //提交信息
    J_form.submit(function () {
        var t = $(this);
        //如果检查通过，直接提交
        if ($("#J_form_check").val() == "1") { return true; }
        //验证填写信息
        var fields = t.find("input:visible");
        var count = fields.length;
        for (var i = 0; i < count; i++) {
            field = fields.eq(i);
            if (field.nextAll(".form-showsuccess").length < 1) {
                field.focus();
                field.blur();
            }
        };
        if (t.find(".form-showerror").length > 0) {
            return false;
        };
        //高亮检查
        var light = $("#J_light");
        light.show();
        light.find(".light-bg").css("height", $(document).height());
        light.find(".light-box").css({ left: ($("#J_form").offset().left + 200), top: $("#J_form").offset().top + 50 });
        light.find(".light-iframe").css("height", $(document).height());
        var html = "<table>";
        var label = t.find("[data-tolight='label']");
        var content = t.find("[data-tolight='content']");
        var count = label.length;
        for (var i = 0; i < count; i++) {
            var a = "<tr><td class='label'>" + label.eq(i).html().replace(/<[^>]*>/ig, "").replace("*", "") + "</td><td>";
            var c = content.eq(i);
            if (c.find("input[type='radio']").length > 0) {
                a += c.find("input[type='radio']:checked").attr("title");
            } else if (c.find("input[type='text']").length > 0) {
                var ct = c.find("input[type='text']");
                var num = ct.length;
                for (var j = 0; j < num; j++) {
                    a += ct.eq(j).val();
                }
            } else if (c.find("select").length > 0) {
                var ct = c.find("select");
                var num = ct.length;
                for (var j = 0; j < num; j++) {
                    a += ct.eq(j).find("option:selected").text() + "/";
                };
                a = a.substring(0, a.length - 1);
            } else {
                a += c.html();
            }
            a += "</td></tr>" ;
            html += a;
        };
        html += "</table>";
        light.find(".light-form").html(html);
        return false;
    });

    J_light.find("button[type='submit']").click(function () {
        $("#J_form_check").val("1");
        J_form.submit();
    });
    J_light.find("button[type='reset']").click(function () {
        J_light.hide();
    });
    J_light.find(".light-close").click(function () {
        J_light.hide();
    });
});